<?php
class DefaultViewAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'index';
		$this->data['menu']	= 'view';
	}
	
	public function execute()
	{
		$id = $this->context->get('id');
		if(!is_numeric($id))
		{
			$id			= base64_decode($id);
			$tmp		= explode('_',$id);
			$id			= $tmp[0];
		}

		//检查是否屏蔽
		$settingService = C('SettingService');
		$forbiddenSetting = $settingService->get('forbidden_setting',true);
		if(!empty($forbiddenSetting['ids']))
		{
			$ids = explode("\n",str_replace("\r", "", $forbiddenSetting['ids']));
			if(in_array($id,$ids))
			{
				//屏蔽的商品
				$this->layout 	= 'result';
				$this->code		= 403;
				return;
			}
		}
		
		$searchKeyService	= new SearchKeyService();
		$searchKey			= $this->getSearchKeyword();
		if($searchKey!='')
		{
			if($this->data['config']['search_key']['enable'])
			{
				$searchKeyService->addOrUpdate($id,$searchKey);
			}
			$this->data['config']['search8']['keyword']	= $searchKey;
		}
		
		$productService	= C('ProductService');
		$info			= $productService->getProductById($id);
		if($info)
		{
			if(!$this->data['isRobot'] && $searchKey!='')
			{
				if($info['click_url']!='')
				{
					//header('Location:'.$info['click_url']);
					//return;
				}
			}
			
			if($this->data['config']['detail']['showcontent'])
			{
				$info['item']['desc']	= str_replace('taobao.net','taobaocdn.com',$info['item']['desc']);
				
				if(preg_match_all('~(http://img\d+\.taobaocdn\.com[^\'">\s]*)([\'">\s])~is', $info['item']['desc'], $m))
				{
					$num	= count($m[1]);
					for($ii=0;$ii<$num;$ii++)
					{
						$info['item']['desc']	= str_replace($m[1][$ii].$m[2][$ii],$this->data['baseurl'].'images/loading.gif'.$m[2][$ii].' u="'.$this->data['cf']->getEncodeImg($m[1][$ii]).'"',$info['item']['desc']);
					}
				}
				
				$info['item']['desc']	= preg_replace('~<a.*?>~is','<a linktype="click-url" href="#" nid="'.$id.'" target="_blank">',$info['item']['desc']);
				$info['item']['desc']	= preg_replace('~<embed .*?>~is','',$info['item']['desc']);
				$info['item']['desc']	= str_replace('alimama',urlencode('alimama'),$info['item']['desc']);
				$info['item']['desc']	= str_replace('taobao',urlencode('taobao'),$info['item']['desc']);
			}
			
			$categoryName		= '';
			if($info['item']['cid']>0)
			{
				$categoryService	= C('CategoryService');
				$parentCategoryList	= $categoryService->getParentList($info['item']['cid']);
				if($parentCategoryList)
				{
					$this->data['parentCategoryList']	= $parentCategoryList;
					$categoryName						= $parentCategoryList[count($parentCategoryList)-1]['name'];
					if(count($parentCategoryList)>=2)
					{
						$this->data['parentCategoryId']	= $parentCategoryList[count($parentCategoryList)-2]['id'];
					}
					if($parentCategoryList[count($parentCategoryList)-1]['is_hide']==1)
					{
						$this->layout 	= 'result';
						$this->code		= 403;
						return;
					}
				}
			}
			
			$seoConfig		= C('SettingService')->get('seo_setting',true);
			$seoTitle		= $this->getSeoStr($seoConfig['detailtitle'],array('title'=>$info['item']['title'],'catName'=>$categoryName,'nick'=>$info['item']['nick'],'price'=>$info['item']['price']));
			$seoKeyword		= $this->getSeoStr($seoConfig['detailkeyword'],array('title'=>$info['item']['title'],'catName'=>$categoryName,'nick'=>$info['item']['nick'],'price'=>$info['item']['price']));
			$seoDescription	= $this->getSeoStr($seoConfig['detaildescription'],array('title'=>$info['item']['title'],'catName'=>$categoryName,'nick'=>$info['item']['nick'],'price'=>$info['item']['price']));
			
			$this->data['seo']['title']			= $seoTitle;
			$this->data['seo']['keyword']		= $seoKeyword;
			$this->data['seo']['description']	= $seoDescription;
			
			$this->data['product']				= $info;
		}
		
		$adService		= new AdService();
		$adList			= $adService->getAdList(true,array('position'));
		$this->data['adList']	= $adList;
		
		if($this->data['config']['search_key']['enable'])
		{
			$searchKeyList		= $searchKeyService->getListByNumIid($id);
		}
		if(empty($searchKeyList))
		{
			$searchKeyList	= array();
		}
		
		$hotkeyList			= array();
		if($info && $info['item']['cid']>0)
		{
			$hotkeyService	= new HotkeyService();
			$hotkeyList		= $hotkeyService->getListByCategoryId($info['item']['cid']);
			if(empty($hotkeyList))
			{
				$hotkeyList	= array();
			}
		}
		
		$this->data['keywordList']	= array_merge($searchKeyList,$hotkeyList);
		
		$oldId = $this->context->get('id');
		$this->data['viewHistoryUrl'] = C('LtUrl')->generate('Default','ViewHistory',array('id'=>$oldId));
		$this->data['currentUrl']	= 'http://'.$this->data['domain'].C('LtUrl')->generate('Default','View',array('id'=>$oldId));
		$this->data['relatedProductUrl'] = C('LtUrl')->generate('Default','RelatedProduct');
		$this->data['searchKey'] = $this->data['cf']->encodeKeyword($searchKey);
	}
}
